﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.GoodsDelivery;
using MINERP.MODEL.GoodsDelivery;
using MINERP.COM;

namespace ERPGoodsDelivery.Delivery
{
    public partial class FrmModify : Form
    {
        //属性
        private static FrmModify _instance;
        public static FrmModify GetInstance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmModify();
                }
                return _instance;
            }
            set { _instance = value; }
        }


        //参数
        private DeliverBackBLL bll = new DeliverBackBLL();
        private BaseInfoBLL baseBll = new BaseInfoBLL();
        private int CurrentDeliverId = 0;

        public FrmModify()
        {
            InitializeComponent();
        }

        private void FrmModify_Load(object sender, EventArgs e)
        {
            try
            {
                BindStation();
                BindStationGoods();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //查询发货单
        private void tsDeliverFind_Click(object sender, EventArgs e)
        {
            try
            {
                FindBillsByDeliver();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //Enter键查询
        private void tsDeliverId_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                FindBillsByDeliver();
            }
        }

        //修改
        private void tsSaveBill_Click(object sender, EventArgs e)
        {
            try
            {
                if (gvBillInfoHeap.SelectedRowsCount == 0)
                {
                    MessageBox.Show("没有账单被选中！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                List<int> billIds = new List<int>();
                int[] indexes = gvBillInfoHeap.GetSelectedRows();
                foreach (int i in indexes)
                {
                    DeliveryBack back = gvBillInfoHeap.GetRow(i) as DeliveryBack;
                    back.IsModified = true;
                    int billId = back.BillId;
                    billIds.Add(billId);
                }
                Cursor = Cursors.WaitCursor;
                DateTime deliverTime = dtDeliverTime.Value;
                DateTime arrivalTime = dtArrivalTime.Value;
                string deliverObject = cbDeliverObject.Text;
                string TuoYunDanCode = txtTuoYunDanCode.Text;
                int num = Convert.ToInt16(spinNum.Text);
                double shopMoney = 0;
                if (txtShopMoney.Text != null && txtShopMoney.Text.Length > 0)
                    shopMoney = Convert.ToDouble(txtShopMoney.Text);
                double selfMoney = 0;
                if (txtSelfMoney.Text != null && txtSelfMoney.Text.Length > 0)
                    selfMoney = Convert.ToDouble(txtSelfMoney.Text);
                double otherMoney = 0;
                if (txtOtherMoney.Text != null && txtOtherMoney.Text.Length > 0)
                    otherMoney = Convert.ToDouble(txtOtherMoney.Text);
                string billRemark = txtRemark.Text;
                string stationRemark = cbStationRemark.Text;
                string receiveStationRemark = cbReceiveStation.Text;
                bll.SaveBillDeliverInfo(billIds, deliverTime, arrivalTime, deliverObject, TuoYunDanCode, num, stationRemark, shopMoney, selfMoney, otherMoney, billRemark, receiveStationRemark);
                MessageBox.Show("保存成功！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                //重新绑定数据
                FindBillsByDeliver();
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        //单击显示当前账单的发货反馈信息
        private void gvBillInfoHeap_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                SetBillDetail(e.RowHandle);
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        //当前账单的发货反馈信息
        private void gvBillInfoHeap_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                SetBillDetail(e.FocusedRowHandle);
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }


        //--------------------------公共方法-----------------------
        #region 绑定账单
        private void BindDeliverHeap(List<DeliveryBack> billInfos)
        {
            try
            {
                gcBillInfoHeap.DataSource = billInfos;
                gcBillInfoHeap.RefreshDataSource();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #region 根据deliverId查询账单
        /// <summary>
        /// 根据deliverId查询账单
        /// </summary>
        private void FindBillsByDeliver()
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                int deliverId = 0;
                try
                {
                    deliverId = Convert.ToInt32(tsDeliverId.Text);
                }
                catch
                {
                    MessageBox.Show("请输入正确的发货单号", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    CurrentDeliverId = 0;
                    tsDeliverId.Focus();
                    tsDeliverId.SelectAll();
                }
                CurrentDeliverId = deliverId;
                int printedStatus = (int)DeliveryStatus.Deliverred;
                List<DeliveryBack> billInfos = bll.GetBillsByDeliverIdAndStatus(deliverId, printedStatus);
                if (billInfos.Count == 0)
                {
                    MessageBox.Show("发货单不存在或该发货单还未发货", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    Cursor = Cursors.Default;
                    return;
                }
                BindDeliverHeap(billInfos);
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        #endregion

        #region 设置账单反馈的详情
        /// <summary>
        /// 设置账单反馈的详情
        /// </summary>
        /// <param name="index"></param>
        private void SetBillDetail(int index)
        {
            try
            {
                DeliveryBack back = gvBillInfoHeap.GetRow(index) as DeliveryBack;
                DateTime deliverTime = back.DeliverTime;
                if (deliverTime.Year < dtDeliverTime.MinDate.Year || deliverTime.Year > dtDeliverTime.MaxDate.Year)
                {
                    dtDeliverTime.Value = DateTime.Now;
                }
                else
                {
                    dtDeliverTime.Value = back.DeliverTime;
                }
                DateTime arrivalTime = back.ArrivalTime;
                if (arrivalTime.Year < dtArrivalTime.MinDate.Year || arrivalTime.Year > dtArrivalTime.MaxDate.Year)
                {
                    dtArrivalTime.Value = DateTime.Now;
                }
                else
                {
                    dtArrivalTime.Value = back.ArrivalTime;
                }
                cbDeliverObject.Text = back.DeliverObject;
                txtTuoYunDanCode.Text = back.TuoYunDanCode;
                spinNum.Value = back.Num;
                cbStationRemark.Text = back.StationRemark;
                cbReceiveStation.Text = back.ReceiveStationRemark;
                txtShopMoney.Text = string.Format("{0:0.##}", back.ShopMoney);
                txtSelfMoney.Text = string.Format("{0:0.##}", back.SelfMoney);
                txtOtherMoney.Text = string.Format("{0:0.##}", back.OtherMoney);
                txtRemark.Text = back.Remark;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 绑定客运站
        /// <summary>
        /// 绑定客运站
        /// </summary>
        private void BindStation()
        {
            try
            {
                List<BaseInfoBLL.StationInfo> stations = baseBll.GetStationInfo();
                cbDeliverObject.DisplayMember = "StationName";
                cbDeliverObject.ValueMember = "StationId";
                cbDeliverObject.DataSource = stations;
                cbDeliverObject.Text = "";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 绑定货运站及其电话
        /// <summary>
        /// 绑定货运站及其电话
        /// </summary>
        private void BindStationGoods()
        {
            try
            {
                List<BaseInfoBLL.StationGoodsInfo> stationGoods = baseBll.GetStationGoodsInfo();
                cbStationRemark.DisplayMember = "StationGoodsNamePhone";
                cbStationRemark.ValueMember = "StationGoodsId";
                cbStationRemark.DataSource = stationGoods;
                cbStationRemark.Text = "";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
